<?php


class VodAsset extends CActiveRecord
{
    const VOD_STATUS_ACTIVE = 1;
    const VOD_STATUS_PENDING = 2;
    const VOD_STATUS_REJECTED = 0;
        
        public $Vvamjdmvic5i;
        public $Vwfk133vtore;
        public $Vf0wtt1q0lik;
        public $Vif53wwzmbl1;
        public $Vizo3j5bossh;
        public $Vbfjw3di3qie;
        public $Vvzhqgqf4t4p;
        public $V0b1ihlordvk;
        public $Var5itcrymel;
        public $Vrwxpiewut2t;
        public $V54a4mp5olw3;
        public $Vjule3ystjlm;
        public $Vwg0zzlimxkj;
        public $Vlmv1jwbdio0;
        public $Vl4ul3ugim3b;
        public $Vtafxfbizgvb;
        public $Vpu0sywyhduk;
        public $Vp0ht3ph2ayz;
        public $Vllfux5zg3zw;
        public $Vexzyyshqy31;


        public $V3cb53weuyqt;      
        public $Vhi05miuvgcl;
        public $Veggaarzpo5h;
        public $Vklfzb2evkkt;
        public $Vbtjta1prh3h;
        public $Vok5egdbefwc;
        public $Vs1c0m51tg0o;
        public $Vpnbkjyse0lr;






	
	public static function model($Vgmajjibziuy=__CLASS__)
	{
		return parent::model($Vgmajjibziuy);
	}

	
	public function tableName()
	{
		return 'vod_asset';
	}

	
	public function rules()
	{
		
		
		return array(
			array('code_name, display_name, create_date', 'required'),
			array('view_count, like_count, dislike_count, rating_count,honor, comment_count, favorite_count, is_series, episode_count, duration,status, vod_stream_count, is_free, image_count, create_user_id, modify_user_id', 'numerical', 'integerOnly'=>true),
			array('rating, price', 'numerical'),
			array('code_name, display_name, display_name_ascii', 'length', 'max'=>200),
			array('tags', 'length', 'max'=>500),
			array('short_description', 'length', 'max'=>300),
			array('description', 'length', 'max'=>3000),
			array('expiry_date, modify_date', 'safe'),
			
			
			array('id, code_name, display_name, status,honor,display_name_ascii, tags, short_description, description, view_count, like_count, dislike_count, rating, rating_count, comment_count, favorite_count, is_series, episode_count, is_active, duration, vod_stream_count, is_free, price, image_count, expiry_date, create_date, modify_date, create_user_id, modify_user_id', 'safe', 'on'=>'search'),
            array('code_name','unique','message'=>'{attribute}:{value} already exists!'),

		);


	}

	
	public function relations()
	{
		
		
		return array(
			'createUser' => array(self::BELONGS_TO, 'BackendUser', 'create_user_id'),
			'modifyUser' => array(self::BELONGS_TO, 'BackendUser', 'modify_user_id'),
			'vodAttributeValues' => array(self::HAS_MANY, 'VodAttributeValue', 'vod_asset_id'),
			'vodCategoryAssetMappings' => array(self::HAS_MANY, 'VodCategoryAssetMapping', 'vod_asset_id'),
                        'vodCategories'=>array(self::HAS_MANY,'VodCategory','vod_category_id', 'through'=>'vodCategoryAssetMappings'),
			'vodEpisodes' => array(self::HAS_MANY, 'VodEpisode', 'vod_asset_id'),                    
			'vodComments' => array(self::HAS_MANY, 'VodComment', 'vod_asset_id'),
			'vodImages' => array(self::HAS_MANY, 'VodImage', 'vod_asset_id'),
			'vodLikeDislikes' => array(self::HAS_MANY, 'VodLikeDislike', 'vod_asset_id'),
			'vodPackageDetails' => array(self::HAS_MANY, 'VodPackageDetail', 'vod_asset_id'),
			'vodRatings' => array(self::HAS_MANY, 'VodRating', 'vod_asset_id'),
			'vodStreams' => array(self::HAS_MANY, 'VodStream', 'vod_asset_id'),
			'vodSubscriberFavorites' => array(self::HAS_MANY, 'VodSubscriberFavorite', 'vod_asset_id'),
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'code_name' => 'Code Name',
			'display_name' => 'Display Name',
			'display_name_ascii' => 'Display Name Ascii',
			'tags' => 'Tags',
			'short_description' => 'Short Description',
			'description' => 'Description',
			'view_count' => 'View Count',
			'like_count' => 'Like Count',
			'dislike_count' => 'Dislike Count',
			'rating' => 'Rating',
			'rating_count' => 'Rating Count',
			'comment_count' => 'Comment Count',
			'favorite_count' => 'Favorite Count',
			'is_series' => 'Is Series',
			'order_in_series' => 'Order In Series',
			'status' => 'Status',
			'duration' => 'Duration',
			'vod_stream_count' => 'Vod Stream Count',
			'is_free' => 'Is Free',
			'price' => 'Price',
			'image_count' => 'Image Count',
			'expiry_date' => 'Expiry Date',
			'create_date' => 'Create Date',
			'modify_date' => 'Modify Date',
			'create_user_id' => 'Create User',
			'modify_user_id' => 'Modify User',
                        'honor' => 'Honor'
			
		);
	}



	public function search()
	{
		
		
                if (isset ($_GET['pageSize']) ) {
                        $Veggaarzpo5hSize = $_GET['pageSize']; 
                        Yii::app()->user->setState('pageAsset',$_GET['pageSize']);
                        } else $Veggaarzpo5hSize = Yii::app()->user->getState('pageAsset');
                        




           
		$V0v5vsnfxezw=new CDbCriteria;
                $V0v5vsnfxezw->distinct = true;
                $V0v5vsnfxezw->select=array('display_name','create_date','is_series');
		$V0v5vsnfxezw->compare('display_name_ascii',  CVietnameseTools::makeSearchableStr($this->display_name),true);
		$V0v5vsnfxezw->compare('tags',$this->tags,true);		
		$V0v5vsnfxezw->compare('vodSeries.display_name_ascii',  CVietnameseTools::makeSearchableStr($this->seriesName),true);
                if ($this->categoryPath!=NULL)
                    $V0v5vsnfxezw->compare('concat("/",vodCategories.path,"/")','/'.substr($this->categoryPath,1).'/',true);
		$V0v5vsnfxezw->compare('is_active',$this->is_active);
		$V0v5vsnfxezw->compare('duration',$this->duration);
		$V0v5vsnfxezw->compare('is_free',$this->is_free);
		$V0v5vsnfxezw->compare('price',$this->price);
		$V0v5vsnfxezw->compare('expiry_date',$this->expiry_date,true);
                if ($this->startDate!= NULL)
                    $V0v5vsnfxezw->addCondition('t.create_date > "'.$this->startDate.'"');
                if ($this->endDate!= NULL)
                    $V0v5vsnfxezw->addCondition('t.create_date < "'.$this->endDate.'"');
               
		$V0v5vsnfxezw->compare('modify_date',$this->modify_date,true);
                $V0v5vsnfxezw->with=array('vodSeries'=>array('select'=>'vodSeries.display_name'),'vodCategories'=>array('select'=>FALSE));
                $V0v5vsnfxezw->together= true;
                
		$Vu2qvgmgucqi = new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw, 'pagination'=>array('pageSize'=>$Veggaarzpo5hSize)
		));
                
                if (isset($_GET['VodAsset_page'])) $Veggaarzpo5h = $_GET['VodAsset_page']; else $Veggaarzpo5h = 1;
                if (isset($_POST['assets']))
                      $Vagm5rdi13go = ','.$_POST['assets'].',';
                 else $Vagm5rdi13go='';   
                $Vbzpaejiy0xf = array();
                foreach ($Vu2qvgmgucqi->data AS $Vlqihxrrpwig) {
                 
                     if ($Vlqihxrrpwig->is_series) {


                     }
                     $Vy5lc1awkxbj = $Vlqihxrrpwig->getPath();
                     $Vgmx3z2zle5m = 1;
                     $Vfcisoqdzruw='';
                   
                   
                     foreach ($Vy5lc1awkxbj as $Vlc1gaaxjqzq) 
                         if ($Vlc1gaaxjqzq->path!=''){
                             $Vfcisoqdzruw .= '<a href="'.Yii::app()->createUrl("admin/vodCategory/view",array("id"=>$Vlc1gaaxjqzq->id)).'">'.$Vlc1gaaxjqzq->getPathName().'</a>'.',';
                             $Vgmx3z2zle5m++;
                         }
                     $Vfcisoqdzruw = substr($Vfcisoqdzruw, 0, -1);    
                     $Vlqihxrrpwig->categoryPath = $Vfcisoqdzruw;
                     if (strpos($Vagm5rdi13go,','.$Vlqihxrrpwig->id.',')!== FALSE) $Vlqihxrrpwig->isChecked = TRUE;
                     $Vbzpaejiy0xf = array_merge($Vbzpaejiy0xf, array($Vlqihxrrpwig));
                }
                $Vu2qvgmgucqi->setData($Vbzpaejiy0xf);
               
                return $Vu2qvgmgucqi;
	}
        
        public function isSeries($Vtqzotzcsg4w,$Vhhc3bf5ojd0) {


        }
        
        public function getPath() {







            $Vq0dgu5sdfqa = $this->vodCategories;
            for ($Vgmx3z2zle5m = 0; $Vgmx3z2zle5m < count($Vq0dgu5sdfqa); $Vgmx3z2zle5m++) 
                for ($Vgmx3z2zle5m1 = 0; $Vgmx3z2zle5m1 < count($Vq0dgu5sdfqa); $Vgmx3z2zle5m1++) {
                    if ($Vq0dgu5sdfqa[$Vgmx3z2zle5m]->path !='' && $Vq0dgu5sdfqa[$Vgmx3z2zle5m1]->path !='' && strpos('/'.$Vq0dgu5sdfqa[$Vgmx3z2zle5m]->path.'/','/'.$Vq0dgu5sdfqa[$Vgmx3z2zle5m1]->path.'/')!==FALSE && $Vgmx3z2zle5m!=$Vgmx3z2zle5m1){
                        $Vq0dgu5sdfqa[$Vgmx3z2zle5m1]->path='';
                    }
                }
            return $Vq0dgu5sdfqa;    
        }
        
        
	public function search2($V02c3ztfj4ey=NULL)
	{
		
		
        
		$V0v5vsnfxezw=new CDbCriteria;
		$V0v5vsnfxezw->compare('display_name',$this->display_name,true);
		$V0v5vsnfxezw->compare('display_name_ascii',$this->display_name_ascii,true);
		
		$Vu2qvgmgucqi = new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw,"pagination"=>array('pageSize'=>'3')
		));
                $Vbzpaejiy0xf = array();
                if (isset ($_POST['listAsset'])) {
                $Vagm5rdi13go = ','.$_POST['listAsset'].',';
                } else $Vagm5rdi13go = $V02c3ztfj4ey.',';
                foreach ($Vu2qvgmgucqi->data AS $Vlqihxrrpwig) {




                          if (strpos($Vagm5rdi13go, ','.$Vlqihxrrpwig->id.',')!==FALSE) {
                             $Vlqihxrrpwig->isInPackage = true;
                          }  
                    
                    $Vbzpaejiy0xf = array_merge($Vbzpaejiy0xf,array($Vlqihxrrpwig));
                }
                    $Vu2qvgmgucqi->setData($Vbzpaejiy0xf);
               
               return $Vu2qvgmgucqi;
	}
        










        
        

































































        
        
    public static function findVODs($Vceaol1c1qxm, $Vtkshvgl1ivp, $Veggaarzpo5h_no, $Veggaarzpo5h_size, $Vmq5toeun05t, $V5as2qkxgfs4 = -1, $Vrlyvhau3qpi = 1) {
    	$V24noxecbnll = "1";
        if($V5as2qkxgfs4 != -1) {
            $V24noxecbnll .= " AND id = ".$V5as2qkxgfs4;
        }
        if($Vrlyvhau3qpi > -1) {
            $V24noxecbnll .= " AND (status = ".$Vrlyvhau3qpi.")"; 
        }
            if (!empty ($Vmq5toeun05t)) {
                $V24noxecbnll .= " AND (t.display_name LIKE '%$Vmq5toeun05t%' OR t.display_name_ascii LIKE '%$Vmq5toeun05t%') ";
            }

            if ($Vceaol1c1qxm != null) {
                $Vfcisoqdzruw = VodCategory::model()->findByPk($Vceaol1c1qxm)->path;
                $V24noxecbnll .= " AND id in (select vod_asset_id from  vod_category_asset_mapping where vod_category_id in (".
                           "select id from vod_category where concat('/',path,'/') like '/$Vfcisoqdzruw/%')) ";
            }

        
        $Vxun03s52uac = Yii::app()->db->createCommand()
                ->selectDistinct('t.id,t.create_date,t.modify_date,t.view_count,t.like_count,t.dislike_count,t.rating,t.rating_count,t.comment_count,t.favorite_count,t.display_name,t.duration, t.honor, t.status, t.view_count, t.episode_count')
                ->from('vod_asset as t')
                ->where($V24noxecbnll) 
                ->limit($Veggaarzpo5h_size, $Veggaarzpo5h_no * $Veggaarzpo5h_size)
                ->order($Vtkshvgl1ivp)
                ->queryAll();
        $Vfx3dng1y1f3 = Yii::app()->db->createCommand()
                ->select('count(id)')
                ->from('vod_asset as t')
                ->where($V24noxecbnll) 
                ->queryScalar();
        
        
        
        
        
        return array(
            'total_result'=>$Vfx3dng1y1f3,
            'keyword'=>$Vmq5toeun05t,
            'page_number'=>$Veggaarzpo5h_no,
            'page_size'=>$Veggaarzpo5h_size,
            'category'=>$Vceaol1c1qxm,
            'total_page'=>($Vfx3dng1y1f3 - ($Vfx3dng1y1f3 % $Veggaarzpo5h_size))/$Veggaarzpo5h_size + (($Vfx3dng1y1f3 % $Veggaarzpo5h_size ===0)?0:1),
            'data'=>$Vxun03s52uac
        );
    }
    
    
    public static function findVOD1s($Vvamjdmvic5i, $Vtkshvgl1ivp, $Veggaarzpo5h_no, $Veggaarzpo5h_size, $Vmq5toeun05t, $V5as2qkxgfs4 = -1, $Vrlyvhau3qpi = 1) {
    	$V24noxecbnll = "1";
        if($V5as2qkxgfs4 != -1) {
            $V24noxecbnll = "id = ".$V5as2qkxgfs4;
        }
        else {
        	if($Vrlyvhau3qpi > -1) {
	            $V24noxecbnll .= " AND (status = ".$Vrlyvhau3qpi.")"; 
        	} 
            if (!empty ($Vmq5toeun05t)) {
                $V24noxecbnll .= " AND (t.display_name LIKE '%$Vmq5toeun05t%' OR t.display_name_ascii LIKE '%$Vmq5toeun05t%') ";
            }

            $V0ed0x4gides = "";
            foreach($Vvamjdmvic5i as $Vceaol1c1qxm) {
            	$Vde2xppeyzpl = VodCategory::model()->findByPk($Vceaol1c1qxm);
            	if($Vde2xppeyzpl != NULL) {
	                $Vfcisoqdzruw = $Vde2xppeyzpl->path;
	                $V0ed0x4gides .= "(concat('/',path,'/') like '/$Vfcisoqdzruw/%') or ";
            	}
            }
            $Vexyoomomnpj = strlen($V0ed0x4gides)- 4;
            $V0ed0x4gides = substr($V0ed0x4gides, 0, $Vexyoomomnpj);
            $V24noxecbnll .= " AND id in (select vod_asset_id from  vod_category_asset_mapping where vod_category_id in (".
                           "select id from vod_category where ".$V0ed0x4gides."))";
        }
        
        $Vxun03s52uac = Yii::app()->db->createCommand()
                ->selectDistinct('t.id,t.create_date,t.modify_date,t.view_count,t.like_count,t.dislike_count,t.rating,t.rating_count,t.comment_count,t.favorite_count,t.display_name,t.duration, t.honor, t.status, t.view_count, t.episode_count')
                ->from('vod_asset as t')
                ->where($V24noxecbnll) 
                ->limit($Veggaarzpo5h_size, $Veggaarzpo5h_no * $Veggaarzpo5h_size)
                ->order($Vtkshvgl1ivp)
                ->queryAll();
        $Vfx3dng1y1f3 = Yii::app()->db->createCommand()
                ->select('count(id)')
                ->from('vod_asset as t')
                ->where($V24noxecbnll) 
                ->queryScalar();
        
        
        
        
        
        return array(
            'total_result'=>$Vfx3dng1y1f3,
            'keyword'=>$Vmq5toeun05t,
            'page_number'=>$Veggaarzpo5h_no,
            'page_size'=>$Veggaarzpo5h_size,
            'category'=>$Vceaol1c1qxm,
            'total_page'=>($Vfx3dng1y1f3 - ($Vfx3dng1y1f3 % $Veggaarzpo5h_size))/$Veggaarzpo5h_size + (($Vfx3dng1y1f3 % $Veggaarzpo5h_size ===0)?0:1),
            'data'=>$Vxun03s52uac
        );
    }
}